package com.pengheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pengheng.entity.Order;
import com.pengheng.entity.Users;
import com.pengheng.service.IOrderService;
import com.pengheng.service.IUsersService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private IUsersService usersService;

    @RequestMapping("/list")
    public Object list() {
        LambdaQueryWrapper<Users> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByAsc(Users::getId);
        return usersService.list(queryWrapper);
    }

    @RequestMapping("/save")
    public Object save(Integer age, String name, Long userId) {
        Users users = new Users();
        users.setAge(age);
        users.setName(name);
        users.setUserId(userId);
        users.setCreateTime(new Date());
        users.setUpdateTime(new Date());
        return usersService.save(users);
    }

    @RequestMapping("/delete")
    public Object delete(Long id) {
        return usersService.removeById(id);
    }

    @RequestMapping("/update")
    public Object update(@RequestBody Users users) {
        return usersService.updateById(users);
    }

}
